/* 
 * Created on 2007-1-14
 * Last modified on 2009-9-23
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.yeqiangwei.commons.ip.IPUtils;
import com.yeqiangwei.commons.util.Pagination.Order;
 
public class ValidatorHelper {
	
	private static final Logger logger = Logger.getLogger(ValidatorHelper.class);
	
	/**
	 * 验证正常请求
	 * @param request
	 * @param response
	 * @param flag
	 * @param interval
	 * @return
	 */
	public static boolean isNormalRequest(HttpServletRequest request,
            HttpServletResponse response, String flag, int interval)
	{
		return !isMaliciousRequest(request,response,flag,interval);
	}
	
	/**
	 * 验证是否为恶意请求
	 * @param request
	 * @param response
	 * @param flag
	 * @param interval 间隔时间
	 * @return
	 */
	public static boolean isMaliciousRequest(HttpServletRequest request,
            HttpServletResponse response, String cookieflag, int interval)
	{
		//给每个用户做个唯一标记		
		String sessionflag = cookieflag + ", " + IPUtils.getIpAddr(request); 
		String firstReqCk = HttpCookieUtils.getValue(request, cookieflag, null);
		String firstReqSess = HttpSessionUtils.getString(request, sessionflag, null);
		String referer = request.getHeader("referer");
		String agent = request.getHeader("User-Agent");
		if(logger.isDebugEnabled()){
			logger.debug("Request referer: " + referer 
					+ ", agent = " + agent
					+ ", ip = " + IPUtils.getIpAddr(request)
					+ ", locale = " + request.getLocale());
		}
		if(isEmpty(referer) && isEmpty(agent) && logger.isDebugEnabled())
		{
			logger.error("This request is likely to be malicious, referer: " + referer 
					+ ", agent = " + agent
					+ ", ip = " + IPUtils.getIpAddr(request)
					+ ", locale = " + request.getLocale()
			);
			return true;
		}
		Long now = DateTimeUtils.getTimeInMillis();
		if(isEmpty(firstReqCk) && isEmpty(firstReqSess)){
			/*
			 * 这是第一次请求
			 * 
			 */
			logger.debug("first request: " + now);
		}
		else{
			/*
			 * 这是第二次以上的请求 
			 */
			Long after = HttpSessionUtils.getObject(request, sessionflag, null);
			if(isEmpty(after)){
				after = TypeChange.stringToLong(
						HttpCookieUtils.getValue(request, cookieflag, null));
			}
			if(logger.isDebugEnabled()){
				logger.debug("...... after first request, now: " + now 
						+ ", after: " + after);
				logger.debug("...... (now < after): " + (now < after));
			}
			if(after == null || now < after){
				after = DateTimeUtils.nSecondsAfterTime(now, interval);
				HttpCookieUtils.setValue(response, null, cookieflag, after, 0);
				HttpSessionUtils.setAttribute(request, sessionflag, after);
				return true;
			}
		}
		Long setAfter = DateTimeUtils.nSecondsAfterTime(now, interval);
		HttpCookieUtils.setValue(response, null, cookieflag, setAfter, 0);
		HttpSessionUtils.setAttribute(request, sessionflag, setAfter);
		return false;
	}
	
	public static boolean isNull(Map<?,?> map){
		return map == null;
	}
	
	public static boolean isEmpty(Map<?,?> map){
		return map == null || map.size() == 0;
	}
	
	public static boolean isEmpty(TreeMap<?,?> map){
		return map == null || map.size() == 0;
	}
	
	public static boolean isEmpty(Set<?> set){
		return set == null || set.isEmpty();
	}
	
	public static boolean isEmpty(List<?> list){
		return list == null || list.size() == 0;
	}
	
	public static boolean isEmpty(Collection<?> collection){
		return collection == null || collection.size() == 0;
	}
	
	public static boolean isEmpty(Object obj){
		return obj == null;
	}
	
	public static boolean isEmpty(StringBuilder sb){
		return sb == null || sb.length() == 0 || sb.toString().length() == 0; 
	}
	
	public static boolean isEmpty(StringBuffer sb){
		return sb == null || sb.length() == 0; 
	}
	
	public static boolean isEmpty(String s){
		return s == null || s.length() == 0;
	}
	
	public static boolean isNotEmpty(String s){
		return !isEmpty(s);
	}
	
	public static boolean isEmpty(Object[] obj){
		return obj == null || obj.length == 0;
	}
	
	public static boolean isEmpty(String[] obj){
		return obj == null || obj.length == 0;
	}
	
	public static boolean isEmpty(byte[] i){
		return i == null || i.length == 0;
	}
	
	public static boolean isEmpty(short[] i){
		return i == null || i.length == 0;
	}
	
	public static boolean isEmpty(long[] i){
		return i == null || i.length == 0;
	}

	public static boolean isNotEmpty(List<?> list) {
		return list != null && list.size() > 0;
	}

	public static boolean isNotEmpty(Set<?> sets) {
		return sets != null && !sets.isEmpty();
	}	
}
